#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  for (int cas = 1; cas <= T; cas ++) {
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i ++) cin >> a[i];
    vector<int> sum(n + 1);
    partial_sum(a.begin(), a.end(), sum.begin());

    vector<vector<i64>> dp(n + 2, vector<i64>(n + 1));
    for (int l = n; l >= 1; l --) {
      dp[l][l] = 0;
      for (int r = l + 1; r <= n; r ++) {
        dp[l][r] = LLONG_MAX;
        for (int k = l; k <= r; k ++) {
          // 栈的处理，让 l 在第 k 位出栈，这样确保 l + 1 到 k 都在前面，k + 1 到 r 都在后面
          dp[l][r] = min(dp[l][r], dp[l + 1][k] + dp[k + 1][r] + a[l] * (k - l) + (sum[r] - sum[k]) * (k - l + 1));
        }
      }
    }
    cout << "Case #" << cas << ": " << dp[1][n] << endl;
  }
}